﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Function : System.Web.UI.Page
    {
        //public string userName = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            //如果在此子系统中有Session
            if (!string.IsNullOrWhiteSpace(Session["user"]?.ToString()) && CheckOnline())
            {
                //userName = Session["user"].ToString();
            }
            else
            {
                string loginUrl = "http://localhost:61756/login";
                Response.Redirect(loginUrl);
            }
        }

        public bool CheckOnline()
        {
            bool isOnline = false;
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:61881/");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("appliation/json"));
            UserInfo user = new UserInfo()
            {
                AppKey = "aaa",
                AppSecret = "sss",
                UserName = "km",
                Password = "123456",
                Ip = "127.0.0.1",
            };
            HttpResponseMessage response = client.PostAsJsonAsync("api/sso/checkonline", user).Result;
            if (response.IsSuccessStatusCode)
            {
                Result obj = new Result();
                var result = response.Content.ReadAsAsync<Result>().Result;
                if (result.Code == 0)
                {
                    isOnline = true;
                }

            }
            return isOnline;
        }
    }
}